package view.frontend;

import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.*;
import view.ViewHandler;
import view.ViewSingleton;
import view.generaldesign.DesignLayout;
import view.menu.MenuLabel;

/**
 *
 * @author Junjie
 */
public class StorageList {

    private JPanel storageList;
    private JPanel storageBanner;
    private JPanel scrollPanel;
    private JScrollPane scrollPane;
    private JPanel sumPanel;

    public StorageList() 
    {
        storageList = new JPanel(null);
        storageList.setBounds(135, 325, 830, 480);
        storageList.setOpaque(true);
        storageList.setBackground(Color.WHITE);
        storageList.setBorder(DesignLayout.T_FIELD_RED);
        storageList.addComponentListener(new ComponentAdapter() 
        {
            @Override
            public void componentShown(ComponentEvent evt)
            {
                ArrayList<MenuLabel> menuLabels = ViewSingleton.getSingletonObject().getMenuLabels();
                for (MenuLabel value : menuLabels)
                {
                    if (value.getMenuLabelText().equals("LAGERSTATUS"))
                    {
                        value.setBackgroundColor();
                    }
                }
                        
                ViewHandler.setPageTitle("Lagerstatus");
                ViewHandler.toggleAllsView("pageTitle", true);
            }
        });
        
        storageBanner = new JPanel(null);
        storageBanner.setBounds(0, 0, 830, 45);
        storageBanner.setOpaque(false);

        JLabel lblProductFamily = new JLabel(" Varegruppe");
        lblProductFamily.setBounds(15, 10, 455, 30);
        lblProductFamily.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));

        JLabel lblAmount = new JLabel("Antal");
        lblAmount.setBounds(480, 10, 100, 30);
        lblAmount.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        lblAmount.setHorizontalAlignment(SwingConstants.CENTER);

        JLabel lblBuyingPrice = new JLabel("Indkøbspris");
        lblBuyingPrice.setBounds(590, 10, 100, 30);
        lblBuyingPrice.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        lblBuyingPrice.setHorizontalAlignment(SwingConstants.CENTER);

        JLabel lblSellingPrice = new JLabel("Salgspris");
        lblSellingPrice.setBounds(700, 10, 100, 30);
        lblSellingPrice.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        lblSellingPrice.setHorizontalAlignment(SwingConstants.CENTER);

        storageBanner.add(lblProductFamily);
        storageBanner.add(lblAmount);
        storageBanner.add(lblBuyingPrice);
        storageBanner.add(lblSellingPrice);

        scrollPanel = new JPanel(null);
        scrollPanel.setBackground(DesignLayout.T_FIELD_BG_COLOR);
        scrollPanel.setBackground(Color.WHITE);
        scrollPanel.setBorder(null);

        scrollPane = new JScrollPane(null);
        scrollPane.setBounds(1, 40, 828, 400);
        scrollPane.setBorder(null);
        scrollPane.setViewportView(scrollPanel);
        scrollPane.setBorder(BorderFactory.createMatteBorder(1, 0, 1, 0, DesignLayout.T_FIELD_BORDER_RED));
        scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
        scrollPane.getVerticalScrollBar().setUnitIncrement(15);
        
        storageList.addComponentListener(new ComponentAdapter() 
        {
            @Override
            public void componentShown(ComponentEvent e)
            {
                updateStorageList();
                updateSumPanel();
            }
        });
        
        sumPanel = new JPanel(null);
        sumPanel.setBounds(1, 445, 828, 30);
        sumPanel.setFont(DesignLayout.STANDARD);
        sumPanel.setOpaque(true);
        sumPanel.setBackground(Color.white);
        
        storageList.add(storageBanner);
        storageList.add(sumPanel);
        storageList.add(scrollPane);
        storageList.setVisible(false);
    }

    public void updateStorageList()
    {
        scrollPanel.removeAll();
        
        model.StorageList storageListModel[] = null;
        try 
        {
            storageListModel = model.handlers.StorageListHandler.getStorageList();
        } 
        catch (ClassNotFoundException ex) 
        {
            
        } 
        catch (SQLException ex) 
        {
            
        }
        
        if(storageListModel != null)
        {
            for (int i = 0; i < storageListModel.length; i++) 
            {
                StorageListPanel panel = new StorageListPanel(storageListModel[i]);
                panel.setBounds(5, 5 + (i * 35), 800, 30);
                scrollPanel.setPreferredSize(new Dimension(830, 30 + (i*35)));
                scrollPanel.add(panel);
            }
        }
        
        scrollPanel.revalidate();
        scrollPanel.repaint();
    }
    
    public void updateSumPanel()
    {
        sumPanel.removeAll();
        
        model.StorageList sumList = null;
        try 
        {
            sumList = model.handlers.StorageListHandler.getStorageListSum();
        } 
        catch (ClassNotFoundException ex) 
        {
            System.out.println(ex.getMessage());
        } 
        catch (SQLException ex) 
        {
            System.out.println(ex.getMessage());
        }
        
        JLabel lblSum = new JLabel(" I alt");
        lblSum.setBounds(15, 0, 455, 30);
        lblSum.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));

        JLabel lblSumNumber = new JLabel(""+sumList.getNumber());
        lblSumNumber.setBounds(480, 0, 100, 30);
        lblSumNumber.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        lblSumNumber.setHorizontalAlignment(SwingConstants.CENTER);
        
        JLabel lblSumPurchasePrice = new JLabel(""+sumList.getPurchasePrice());
        lblSumPurchasePrice.setBounds(590, 0, 100, 30);
        lblSumPurchasePrice.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        lblSumPurchasePrice.setHorizontalAlignment(SwingConstants.CENTER);
        
        JLabel lblSumSalesPrice = new JLabel(""+sumList.getSalesPrice());
        lblSumSalesPrice.setBounds(700, 0, 100, 30);
        lblSumSalesPrice.setFont(DesignLayout.STANDARD.deriveFont(Font.BOLD));
        lblSumSalesPrice.setHorizontalAlignment(SwingConstants.CENTER);
        
        sumPanel.add(lblSum);
        sumPanel.add(lblSumNumber);
        sumPanel.add(lblSumPurchasePrice);
        sumPanel.add(lblSumSalesPrice);
        
        sumPanel.revalidate();
        sumPanel.repaint();
    }
    
    public JPanel getPanel() {
        return storageList;
    }
}
